﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;
using System.Security.Principal;

public partial class SelectUserTypeEN : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnEnter_Click(object sender, EventArgs e)
    {
        if (rbEmployee.Checked)
        {
            Response.Redirect("loginen.aspx");
        }
        else
        {
            IntPtr token = IntPtr.Zero;
            

            try
            {
                // Create a token for DomainName\Bob
                // Note: Credentials should be encrypted in configuration file
                bool result = LogonUser(System.Web.Configuration.WebConfigurationManager.AppSettings["username"],
                                        System.Web.Configuration.WebConfigurationManager.AppSettings["domain"],
                                        System.Web.Configuration.WebConfigurationManager.AppSettings["pass"],
                                        LogonSessionType.Network,
                                        LogonProvider.Default,
                                        out token);
                if (result)
                {
                    WindowsIdentity id = new WindowsIdentity(token);
                    HttpContext.Current.User = new WindowsPrincipal(id);
                    id.Impersonate();
                }
            }
            catch
            {
            }

            Response.Redirect("loginen.aspx");
        }

    }



    [DllImport("advapi32.dll", SetLastError = true)]
    static extern bool LogonUser(
      string principal,
      string authority,
      string password,
      LogonSessionType logonType,
      LogonProvider logonProvider,
      out IntPtr token);
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool CloseHandle(IntPtr handle);
    enum LogonSessionType : uint
    {
        Interactive = 2,
        Network,
        Batch,
        Service,
        NetworkCleartext = 8,
        NewCredentials
    }
    enum LogonProvider : uint
    {
        Default = 0, // default for platform (use this!)
        WinNT35,     // sends smoke signals to authority
        WinNT40,     // uses NTLM
        WinNT50      // negotiates Kerb or NTLM
    }

}